package com.tust.controller;

import com.tust.domain.Teaching;
import com.tust.service.TeachingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class TeachingController {
    private String userName;
    @Autowired
    private TeachingService teachingService;

    @ResponseBody
    @RequestMapping("/selectTeachingAll.do")
    public List<Teaching> selectTeachingAll(String userName) {
        List<Teaching> teachings = teachingService.selectTeachingAll();
        this.userName = userName;
        return teachings;
    }

    @ResponseBody
    @RequestMapping("/insertTeaching.do")
    public String insertTeaching(String courseNum, String teacherNum,
                                 String schoolYear, String schoolSemester,
                                 String teachTimes, String coachTimes,
                                 String leadExperimentTimes) {
        String msg =null;
        Teaching newTeaching = new Teaching(courseNum, teacherNum, schoolYear,
                schoolSemester, teachTimes, coachTimes, leadExperimentTimes);
        List<Teaching> teaching = teachingService.selectTeaching(newTeaching);
        if (teaching.size() == 1) {//授课存在
            msg = "teachingExist";
        } else {
            if (teachingService.insertTeaching(newTeaching) == 1) {//插入成功
                msg = "insertSuccess";
            } else {//插入失败
                msg = "insertFalse";
            }
        }
        return msg;
    }

    @ResponseBody
    @RequestMapping("/updateTeaching.do")
    public String updateTeaching(String oldCourseNum, String oldTeacherNum,
                                 String oldSchoolYear, String oldSchoolSemester,
                                 String oldTeachTimes, String oldCoachTimes,
                                 String oldLeadExperimentTimes,
                                 String courseNum, String teacherNum,
                                 String schoolYear, String schoolSemester,
                                 String teachTimes, String coachTimes,
                                 String leadExperimentTimes) {
        String msg = null;
        Teaching newTeaching = new Teaching(courseNum, teacherNum, schoolYear,
                schoolSemester, teachTimes, coachTimes, leadExperimentTimes);
        List<Teaching> teaching = teachingService.selectTeaching(newTeaching);
        if (teaching.size() == 1 && (!oldCourseNum.equals(courseNum)
                || !oldTeacherNum.equals(teacherNum) || !oldSchoolYear.equals(schoolYear)
                || !oldSchoolSemester.equals(oldSchoolSemester))) {//新授课存在
            msg = "newTeachingExist";
        } else {
            Teaching oldTeaching = new Teaching(oldCourseNum, oldTeacherNum, oldSchoolYear,
                    oldSchoolSemester, oldTeachTimes, oldCoachTimes, oldLeadExperimentTimes);
            if (teachingService.updateTeaching(oldTeaching, newTeaching) == 1) {//修改成功
                msg = "updateSuccess";
            } else {//修改失败
                msg = "updateFalse";
            }
        }
        return msg;
    }

    @ResponseBody
    @RequestMapping("/selectTeaching.do")
    public List<Teaching> selectTeaching(String courseNum, String teacherNum,
                                         String schoolYear, String schoolSemester,
                                         String teachTimes, String coachTimes,
                                         String leadExperimentTimes) {
        List<Teaching> teaching = null;
        if (!courseNum.equals("")) {//查询课程号
            teaching = teachingService.selectTeachingByCourseNum(courseNum);
        } else if (!teacherNum.equals("")) {//查询教师号
            teaching = teachingService.selectTeachingByTeacherNum(teacherNum);
        } else if (!schoolYear.equals("")) {//查询学年
            teaching = teachingService.selectTeachingBySchoolYear(schoolYear);
        } else if (!schoolSemester.equals("")) {//查询学期
            teaching = teachingService.selectTeachingBySchoolSemester(schoolSemester);
        } else if (!teachTimes.equals("")) {//查询主讲时数
            teaching = teachingService.selectTeachingByTeachTimes(teachTimes);
        } else if (!coachTimes.equals("")) {//查询辅导时数
            teaching = teachingService.selectTeachingByCoachTimes(coachTimes);
        } else if (!leadExperimentTimes.equals("")) {//查询带实验时数
            teaching = teachingService.selectTeachingByLeadExperimentTimes(leadExperimentTimes);
        }
        return teaching;
    }

    @ResponseBody
    @RequestMapping("/deleteTeaching.do")
    public String deleteTeaching(String courseNum, String teacherNum,
                                 String schoolYear, String schoolSemester,
                                 String teachTimes, String coachTimes,
                                 String leadExperimentTimes) {
        String msg = null;
        Teaching teaching = new Teaching(courseNum, teacherNum, schoolYear,
                schoolSemester, teachTimes, coachTimes, leadExperimentTimes);
        if (teachingService.deleteTeaching(teaching) == 1) {//删除成功
            msg = "deleteSuccess";
        } else {//删除失败
            msg = "deleteFalse";
        }
        return msg;
    }

    @RequestMapping("/teacherSelectionRefresh.do")
    public ModelAndView teacherSelectionRefresh() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("teacherSelection");
        mv.addObject("userName", userName);
        mv.addObject("modelName", "教师授课操作");
        mv.addObject("animate","false");
        return mv;
    }
}
